﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using LoungeRepo.Contracts.EntityStore;
using LoungeRepo.Contracts.Normalizer;
using LoungeRepo.Contracts.Serialization;

namespace LoungeRepo.Serialization
{
    public class BinarySerializer : INormalizedEntitySerializer
    {
        public IStreamedEntity Serialize(INormalizedEntity entity)
        {
            StreamedNormalizedEntity stream = new StreamedNormalizedEntity
                                                  {
                                                      Id = entity.Id,
                                                      Partition = entity.Partition,
                                                      Blob = new MemoryStream()
                                                  };

            new BinaryFormatter().Serialize(stream.Blob, entity);

            return stream;
        }


        public INormalizedEntity Deserialize(IStreamedEntity stream)
        {
            return (INormalizedEntity) new BinaryFormatter().Deserialize(stream.Blob);
        }
    }
}
